/* vi: set et sw=4 ts=4 cino=t0,(0: */
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of gsignond
 *
 * Copyright (C) 2012 Intel Corporation.
 *
 * Contact: Alexander Kanavin <alex.kanavin@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

/**
 * SECTION:gsignond-oauth-plugin-utils
 * @short_description: miscellaneous utility functions
 *
 * Miscellaneous utility functions used by the OAuth plugin go here
 */


 
#include <glib.h>
#include <gnutls/gnutls.h>
#include <gnutls/crypto.h>
#include "gsignond-oauth-plugin-utils.h"
#include <stdlib.h>

/**
 * gsignond_oauth_plugin_generate_random_data:
 * @len: length of random data. 
 *
 * Generates random base64 encoded data of at least @len bytes. This 
 * function is used for generating nonce in oauth1 implementation
 * and state in oauth2 implementation.
 *
 * Returns: (transfer full): a base64 encoded random string or NULL if error.
 *
 */
gchar* gsignond_oauth_plugin_generate_random_data(size_t len)
{
    void* random_data = malloc(len);

    int res = gnutls_rnd(GNUTLS_RND_NONCE, random_data, len);
    if (res != 0) {
        free(random_data);
        return NULL;
    }
    
    gchar* out = g_base64_encode(random_data, len);
    free(random_data);    
    return out;
}
